/*
 * Copyright 2012 Adrian Sulumberchean

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package ro.sands.requests.android.server;

import org.json.JSONArray;
import org.json.JSONException;

/**
 * 
 * @author Adrian Sulumberchean
 *
 */
public class Request {

//	private String userId;
	private String requestId;
	private JSONArray request;
	private JSONArray requestData;
	
	public Request(String requestId){
		this(requestId, null);
	}
	
	public Request(String requestId, JSONArray requestData) {
		this.requestId = String.valueOf(requestId);		
		this.requestData = requestData;	
		
		init();
		buildRequest();		
	}
	
	private void init() {
		request = new JSONArray();		
	}

	private void buildRequest() {				
//		request.put(userId);										//userId
		request.put(String.valueOf(requestId));						//requestId
		
		if(requestData != null)										//body
			request.put(requestData);	
	}
	
	/**
	 * @return the request as String
	 */
	public String getRequestStr(){
		if(request == null){
			System.err.println("Request is null");
			return "";
		}
		
		return request.toString();
	}
	
	public String toString() {
		return "Request: " + getRequestStr();
	}

//	/**
//	 * @return the userId
//	 */
//	public String getUserId() {
//		return userId;
//	}
//	/**
//	 * @param userId the userId to set
//	 */
//	protected void setUserId(String userId) {
//		this.userId = userId;
//		try {
//			request.put(0, userId);
//		} catch (JSONException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	/**
	 * @return the requestId
	 */
	public String getRequestIdAsString() {
		return String.valueOf(requestId);
	}
	
	public String getRequestId() {
		if(requestId.contains("-")){
			return requestId.substring(0, requestId.indexOf("-"));
		}
		
		return requestId;
	}
	
	protected void setRequestId(int requestId){
		setRequestId(String.valueOf(requestId));
	}
	
	/**
	 * @param requestId the requestId to set
	 */
	protected void setRequestId(String requestId) {
		this.requestId = requestId;
		try {
			request.put(1, requestId);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void put(Object... values){
		if(requestData == null){
			requestData = new JSONArray();
			request.put(requestData);	
		}
		for (Object value : values) {
			requestData.put(value);
		}
	}
	
	public void putInRoot(Object... values){
		for (Object value : values) {
			request.put(value);
		}
	}

}
